Skip to content

feat(hook-manager): add a pre-commit adapter - #2108

Merged
potiuk merged 2 commits into
apache:mainfrom
wynneyin:feat/hook-manager-pre-commit-adapter
Nov 26, 2026
Merged

potiuk merged 2 commits into
apache:mainfrom
wynneyin:feat/hook-manager-pre-commit-adapter

Conversation

@wynneyin

Copy link
Copy Markdown
Contributor

Summary

  • Register pre-commit in tools/hook-manager/registry.json with the hide-global-config install strategy. feat(hook-manager): add a lefthook adapter #1895 landed that strategy for lefthook, so the shim half of hook-manager: add a pre-commit adapter #1820 needed no code change: a bare install still refuses with Cowardly refusing to install hooks with core.hooksPath set, and the shim's GIT_CONFIG_GLOBAL=/dev/null hides the global value.
  • Implement the Home decision the contract left open. PRE_COMMIT_HOME reaches os.path.realpath unexpanded, so a ~ value would resolve under the session's working directory and the committed settings cannot name the sandbox home. Option 1 from the issue: sandbox-add-project-root.sh writes the absolute path into the project-local .claude/settings.local.json, generated from the registry, and removes it from a project that does not use the manager. sandbox-lint accepts an absolute value and rejects a literal ~.
  • Document the adapter, the non-expanding home rule, and the "Cowardly refusing" failure with its fix.

Type of change

  • Tool / bridge contract (tools/hook-manager/*.md, tools/agent-isolation/*.sh)
  • Python package (tools/dev/, tools/sandbox-lint/)
  • Cross-cutting (RFC, AGENTS.md, sandbox, privacy-LLM)
  • Documentation (docs/, README.md, CONTRIBUTING.md)
  • CI / dev loop (prek, workflows, validators)

Test plan

  • uv run pytest in tools/dev and tools/sandbox-lint: all pass (tools/dev covers the registry validation, the new helper block and the committed-settings rule; tools/sandbox-lint covers absolute-versus-~).
  • uv run pytest in tools/agent-isolation for test_hook_manager_shim.py, test_sandbox_add_project_root.py and test_git_hook_dispatcher.py: all pass. The container-gateway and gpg-overlay files fail on this macOS host with OSError: AF_UNIX path too long before any assertion; they are untouched by this change.
  • Real pre-commit 4.7.0 through the committed shim, with a global core.hooksPath pointing at a shared hooks dir:
    • bare pre-commit install: [ERROR] Cowardly refusing to install hooks with core.hooksPath set., exit 1;
    • ~/.claude/bin/pre-commit install: pre-commit installed at .git/hooks/pre-commit, exit 0, shared dir empty, generated hook carries the pre-commit marker;
    • install from a linked worktree: lands in the main checkout's .git/hooks/pre-commit;
    • sandbox-add-project-root.sh --dry-run in this repository proposes "PRE_COMMIT_HOME": "/home/<user>/.cache/pre-commit-sandbox" in settings.local.json.
  • prek run --files <changed files> --skip workspace-pytest: every hook passes. ruff reports pr-management and sourcehut only under a locally resolved ruff 0.17.5; with the locked 0.17.3 those packages pass (ruff check tools/pr-management tools/sourcehut → all checks passed), and neither is touched here.

RFC-AI-0004 compliance

  • Sandbox — no new unrestricted host access. The sandbox home stays under ~/.cache/, the default home stays denied, and the host-only home stays under ~/.local/share/.

Linked issues

Closes #1820

Notes for reviewers

  • The decision in step 2. A committed env.PRE_COMMIT_HOME cannot be portable: pre-commit keeps ~ literal, and an absolute path differs per machine. So expands_tilde: false is a registry fact, sync-hook-managers.py stops requiring the committed entry for it, and the helper writes the value per project. The contract's Home section now states this.
  • Why an absent value is not a lint error. This repository has a root .pre-commit-config.yaml (prek uses the same file names), so it counts as using pre-commit, and CI has no settings.local.json. Failing on the absent value would break CI for a per-machine file. The safety argument still holds: the default home is denied for every registered manager, so a sandboxed run without a home of its own fails instead of writing the home a terminal's commits use.
  • doctoc touched one extra line in docs/setup/sandbox-troubleshooting.md: a heading the committed TOC was already missing. It came along because this PR edits the file.
  • pre-commit init-templatedir is not offered as a workaround anywhere in the docs: it forces overwrite=True, which moves a foreign hook to .legacy and then deletes it, taking the dispatcher with it.

@github-actions github-actions Bot added family:setup setup-* skills family:tools tools/* family:docs Docs, MISSION.md, READMEs family:ci .github workflows, prek, validators capability:platform Framework / agent substrate skills (install, verify, doctor, override, status, setup bootstrap) substrate:sandbox Tool substrate: agent isolation, egress control, settings audit substrate:framework-dev Tool substrate: build / validate / eval the framework itself contract:hook-manager Tool capability: git-hook managers under the secure setup (install target, sandbox / host homes) family:dev Repo dev: .asf.yaml, spec-loop specs, Node manifests, .claude/settings.json labels Nov 15, 2026
@wynneyin
wynneyin force-pushed the feat/hook-manager-pre-commit-adapter branch from 68f2454 to 1ecd7b8 Compare November 19, 2026 13:59
@wynneyin

Copy link
Copy Markdown
Contributor Author

Rebased onto main at fdca3ace and force-pushed; the PR is no longer CONFLICTING.

Two conflicts, both mechanical:

  • docs/setup/sandbox-troubleshooting.md: main added the tools/vetted-ops entry at the same TOC position this PR used for the git status entry. I kept both sections in the order they appear in the body and regenerated the doctoc block for the merged file, so the anchor numbers moved (symptom-16/symptom-17, notes-15). The generator was checked against main first: it reproduces all 86 existing TOC lines byte for byte before being applied to the merged file.
  • plugins/magpie-setup/skills/setup/setup_preflight/isolated_fingerprint.py: the constant committed on the old base (2dfca14a) does not match the rebased tree because main changed files under tools/agent-isolation. Recomputed it the way write-constant does, over tracked file contents only (this checkout's filesystem adds AppleDouble ._* files that a direct tree walk would hash), which yields sha256:898f94a7d62cda6f. As a check, the same computation returns 2dfca14a at the old head 68f24549 and 2b2d856e on main, matching the constants those trees carry.

No source behavior changed. Hosted tests did not run for the old head; this push is the first run on a mergeable base.

@KatalKavya96

Copy link
Copy Markdown
Member

@wynneyin, thanks for the PR. It is still marked as a draft, so reviewers have not picked it up: when you'd like a full review, rebase it (docs/setup/sandbox-troubleshooting.md and the generated isolated_fingerprint.py conflict with main; the red prek run predates #2202 and should clear) and click "Ready for review".

Two early notes from a quick read, in case they are useful before then:

  1. detect.hook_marker: "pre-commit" (tools/hook-manager/registry.json:168) also matches other managers' hooks: a hook installed by prek install contains --hook-type=pre-commit. A real pre-commit hook carries # File generated by pre-commit: https://pre-commit.com; the marker pre-commit.com matches only that and passes _MARKER.
  2. On unset, sandbox-add-project-root.sh:441 removes any PRE_COMMIT_HOME from settings.local.json, including one the operator set themselves; removing it only when the value is the helper's own sandbox path would keep the cleanup to what the helper owns.

@wynneyin
wynneyin force-pushed the feat/hook-manager-pre-commit-adapter branch from 1ecd7b8 to 5199bae Compare November 25, 2026 20:29
@wynneyin
wynneyin marked this pull request as ready for review November 25, 2026 20:29
@wynneyin

Copy link
Copy Markdown
Contributor Author

Rebased onto main (9deb6d4f) and marked ready; both of your notes are applied, and the push is the first run on this base.

Rebase. Two conflicts, both mechanical:

  • docs/setup/sandbox-troubleshooting.md — main added the lefthook install entry where this PR added the pre-commit install one. Both sections are in the merged body, so the table of contents now lists both and every anchor below them shifted by one. I regenerated the whole doctoc block rather than patching the numbers by hand: the generator reproduces main's block byte for byte before it is applied.
  • plugins/magpie-setup/skills/setup/setup_preflight/isolated_fingerprint.py — recomputed for the merged tree (one file under SOURCES changed, tools/agent-isolation/sandbox-add-project-root.sh), now sha256:7e4b9617947a9714, in its own commit. The method is the one write-constant uses (tracked file SOURCES files, path and contents each followed by NUL); the same routine returns 2b2d856e6cb8f098 at fdca3ace and 898f94a7d62cda6f at the previous head of this branch, matching the constants those trees carried.

Note 1 — the marker. detect.hook_marker is now pre-commit.com. I checked it against the real binaries instead of taking the text on trust:

pre-commit 4.7.0   hook line 2:  # File generated by pre-commit: https://pre-commit.com
prek       0.5.10  hook line 13: exec "$PREK" hook-impl --hook-dir "$HERE" --script-version 4 --hook-type=pre-commit -- "$@"

so a prek hook contains pre-commit and no pre-commit.com. The helper's tests carry both fixtures now: a generated pre-commit hook is detected, a generated prek hook is not. The registry README row and the troubleshooting entry say which text is matched and why.

Note 2 — the cleanup. On unset, sandbox-add-project-root.sh now drops the PRE_COMMIT_HOME entry only when its value is the absolute sandbox home the helper wrote itself; a value the operator set is left alone. The output line carries that value (unset<TAB>VAR<TAB>home), and the jq filter compares against it. One test covers the helper's own value being removed, another that a foreign value survives.

Tests: pytest over tools/agent-isolation/tests/{test_sandbox_add_project_root,test_hook_manager_shim,test_git_hook_dispatcher}.py, tools/dev/tests/test_sync_hook_managers.py and tools/sandbox-lint/tests — 690 passed.

No further changes made: no sign-off added and nothing else in the tree touched. The CI run on this push will say whether the prek failure you mentioned cleared with the new base.

@KatalKavya96 KatalKavya96 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wynneyin, thanks for the PR.
Three things stood out: you checked the new pre-commit.com marker against the real pre-commit 4.7.0 and prek 0.5.10 hooks instead of trusting the text, and the helper tests now carry both fixtures; the unset cleanup removes only the value the helper wrote itself, with a test for each side; and you regenerated the doctoc block and the isolated-setup fingerprint with the method spelled out, so both are easy to check.

Should change

  • Commit trailer. Both commits (fe9d0d90, 5199bae5) end with Co-Authored-By: Claude Code <noreply@anthropic.com>. AGENTS.md says:

    MUST NOT use Co-Authored-By: with an AI agent as co-author. [...] Use a Generated-by: trailer instead.

    GitHub carries commit co-authors into the squash message, so this would land on main. Rewording both commit messages to a Generated-by: trailer and force-pushing fixes it.

Smaller points

  • An operator's PRE_COMMIT_HOME is overwritten while the repository uses pre-commit (inline on sandbox-add-project-root.sh:442).
  • Spec. tools/spec-loop/specs/agent-isolation-sandbox.md describes the prek and lefthook adapters but not pre-commit yet; the lefthook adapter (#1895) updated it in the same PR.

CI has not run on this head yet; I have approved the workflows so it runs alongside your fixes.


This review was drafted by an AI-assisted tool and
confirmed by an Apache Magpie maintainer. After you've
addressed the points above and pushed an update, an Apache Magpie
maintainer — a real person — will take the next look
at the PR. The findings cite the project's review criteria;
if you think one of them is mis-applied, please reply on the
PR and a maintainer will weigh in.

More on how Apache Magpie handles maintainer review:
Contributing guide.

@wynneyin
wynneyin force-pushed the feat/hook-manager-pre-commit-adapter branch from 5199bae to 3c9c189 Compare November 26, 2026 04:47
@wynneyin

Copy link
Copy Markdown
Contributor Author

All three points are addressed on 3c9c1893; the approved CI run on 5199bae5 had gone green (tests, prek, rat, CodeQL, Security Analysis split the duplicate trigger and cancelled the second set), so this push only adds the fixes.

Commit trailer. My mistake: I added Co-Authored-By: where AGENTS.md says Generated-by:. Both commits were rewritten and force-pushed; git log origin/main..HEAD --format=%B | grep -c Co-Authored-By returns 0, and both carry only:

Generated-by: Claude Code (deepseek-v4.1-flash)

The trailer went in with git commit -F <file> --trailer, not typed into the body.

Operator value while the repository uses pre-commit. Applied your swap — $envs + (.env // {}) — so the file wins and the helper only fills a gap. New test test_a_value_the_operator_chose_survives_a_repository_that_uses_it (a .pre-commit-config.yaml plus a PRE_COMMIT_HOME already in settings.local.json). I checked it is not vacuous: with the old operand order restored, that test fails with elsewhere/pre-commit replaced by home/.cache/pre-commit-sandbox; with the change it passes. The helper's header comment now says a value already in the file is the operator's in both directions.

Spec. tools/spec-loop/specs/agent-isolation-sandbox.md now records the adapter beside prek and lefthook: the hide-global-config strategy, why the committed settings carry no env entry for a home the tool will not expand, the helper write/remove rule with the operator's value kept, and the https://pre-commit.com marker (a prek hook carries --type=pre-commit instead). The registry sentence lists it too.

The fingerprint moved with the helper, so the chore commit now sets sha256:cee6b229ebd21a0d; the method and the two cross-checks are unchanged.

pytest tools/agent-isolation/tests/test_sandbox_add_project_root.py tools/agent-isolation/tests/test_hook_manager_shim.py tools/agent-isolation/tests/test_git_hook_dispatcher.py tools/dev/tests/test_sync_hook_managers.py tools/sandbox-lint/tests — 691 passed.

@wynneyin

Copy link
Copy Markdown
Contributor Author

Heads-up on CI: the runs for 3c9c1893 came up action_required again — the approval does not carry over to a new push from the fork, so each head needs one click. prek, tests, rat, CodeQL, sandbox-lint, Labeler signal and Security Analysis are all queued behind it.

The changes themselves are in; the reply above describes them item by item.

@potiuk
potiuk force-pushed the feat/hook-manager-pre-commit-adapter branch from 3c9c189 to 8f2831f Compare November 26, 2026 16:02

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, all three points from the earlier review are addressed: operator PRE_COMMIT_HOME is kept, the spec is updated, and the trailers are right. I rebased the branch onto main (dropping the fingerprint-only commit, which conflicted) and pushed a small fixup (8f2831f) that regenerates isolated_fingerprint.py and corrects the spec's flag to --hook-type=pre-commit. Please pull (or reset to the branch) before any further change.


This review was drafted by an AI-assisted tool and
confirmed by an Apache Magpie maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.

More on how Apache Magpie handles maintainer review:
Contributing guide.

@potiuk
potiuk enabled auto-merge (squash) November 26, 2026 16:05
@potiuk
potiuk dismissed KatalKavya96’s stale review November 26, 2026 16:18

All three points are addressed on the current head (checked against the code); dismissing so the PR can merge. Thanks for the review.

@potiuk
potiuk disabled auto-merge November 26, 2026 22:42
@potiuk
potiuk force-pushed the feat/hook-manager-pre-commit-adapter branch from 8f2831f to b92b03b Compare November 26, 2026 23:05
wynneyin and others added 2 commits November 27, 2026 00:06
Contract: tools/hook-manager/README.md. Closes apache#1820.

pre-commit writes to the common hooks dir but refuses to install while
`git config core.hooksPath` returns anything, so it takes the
`hide-global-config` strategy that apache#1895 landed for lefthook; the shim
half of the issue already existed.

What did not exist is a home variable that cannot spell `~`.
`PRE_COMMIT_HOME` reaches `os.path.realpath` unexpanded, so the committed
settings cannot name the sandbox home and a `~` value would resolve under
the session's working directory. Option 1 from the issue, implemented:

- registry: accept `home.expands_tilde: false` and register pre-commit
  with `nested: false` and `hook_marker: "pre-commit.com"`, the URL in
  the hook it generates (a `prek` hook carries only
  `--hook-type=pre-commit`, so the bare name would match another
  manager's install);
- sync-hook-managers.py: carry the flag into the lint tuple, stop
  requiring the committed `env` entry for such a home (an absolute path
  is not portable), and generate a fourth block — adapter, variable and
  sandbox home — into sandbox-add-project-root.sh;
- sandbox-add-project-root.sh: write the absolute sandbox home into the
  project-local settings.local.json when the repository uses the manager,
  and remove that value from one that does not; a value already in the
  file is the operator's, so it is kept where it is in both directions
  (detection follows the contract: root config, tracked nested config, or
  an installed hook naming the manager);
- tools/spec-loop/specs/agent-isolation-sandbox.md: record the adapter
  beside the prek and lefthook ones, as apache#1895 did for lefthook;
- sandbox-lint: for a home the tool does not expand, reject a `~` value
  and accept the absolute one; an absent value is not an error because the
  helper writes it per machine and the default home stays denied, so a
  sandboxed run without it fails instead of escaping.

Verified against pre-commit 4.7.0 through the built tree: a bare install
refuses with "Cowardly refusing to install hooks with `core.hooksPath`
set", the shim installs to `$(git rev-parse --git-common-dir)/hooks/`
leaving the shared dir empty, and a linked worktree lands in the main
checkout's hooks. The dispatcher now exports a host-only PRE_COMMIT_HOME
from the generated table, and the helper dry-run for this repository
proposes `~/.cache/pre-commit-sandbox` as an absolute path. Marker:
pre-commit 4.7.0's hook carries `https://pre-commit.com`; prek 0.5.10's
carries `--hook-type=pre-commit` and no `pre-commit.com`, which is what
the two hook fixtures in the helper's tests now assert.

Tests: registry validation, the generated helper block, the helper's
write/remove behaviour (including a foreign value surviving), the shim's
hide-global-config path with a pre-commit-named stand-in, the dispatcher's
host home, and the lint's absolute-versus-`~` rule.
`pytest tools/agent-isolation/tests/test_sandbox_add_project_root.py
tools/agent-isolation/tests/test_hook_manager_shim.py
tools/agent-isolation/tests/test_git_hook_dispatcher.py
tools/dev/tests/test_sync_hook_managers.py tools/sandbox-lint/tests`
passes on the rebased tree: 690 passed.

Generated-by: Claude Code (deepseek-v4.1-flash)
…ok flag

After the rebase onto main, the isolated-setup fingerprint is
regenerated, and the spec names the flag a prek-installed hook actually
carries (`--hook-type=pre-commit`).

Generated-by: Claude Opus 5
@potiuk
potiuk force-pushed the feat/hook-manager-pre-commit-adapter branch from b92b03b to 0445313 Compare November 26, 2026 23:06
@potiuk

potiuk commented Nov 26, 2026

Copy link
Copy Markdown
Member

@wynneyin I rebased this onto main (e36a08e0) and force-pushed, since main moved again right after the last round. Three conflicts:

  • sandbox-add-project-root.sh: main added --argjson parent/unsafe to the same jq calls that gained envs/stale_vars here, so both sets are kept.
  • test_sandbox_add_project_root.py: I kept main's formatting and re-added TestHookManagerHomes. The rest of the diff to this file was line-wrapping only.
  • isolated_fingerprint.py: regenerated for the rebased tree.

Please pull (git fetch && git reset --hard origin/feat/hook-manager-pre-commit-adapter) before pushing anything else.

@potiuk
potiuk merged commit 8121bef into apache:main Nov 26, 2026
15 checks passed

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pre-commit adapter is complete across the dispatcher, shim, project-root helper, registry and lint baseline, and the rebase onto current main keeps both sides of the conflicting changes; CI is green.


This review was drafted by an AI-assisted tool and
confirmed by an Apache Magpie maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.

More on how Apache Magpie handles maintainer review:
Contributing guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

capability:platform Framework / agent substrate skills (install, verify, doctor, override, status, setup bootstrap) contract:hook-manager Tool capability: git-hook managers under the secure setup (install target, sandbox / host homes) family:ci .github workflows, prek, validators family:dev Repo dev: .asf.yaml, spec-loop specs, Node manifests, .claude/settings.json family:docs Docs, MISSION.md, READMEs family:setup setup-* skills family:tools tools/* substrate:framework-dev Tool substrate: build / validate / eval the framework itself substrate:sandbox Tool substrate: agent isolation, egress control, settings audit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hook-manager: add a pre-commit adapter

3 participants